//
//  lxMapViewController.m
//  luodansc
//
//  Created by 倫 星 on 2012/11/02.
//  Copyright (c) 2012年 方正株式会社. All rights reserved.
//

#import "lxMapViewController.h"

@interface lxMapViewController ()

@end

@implementation lxMapViewController
@synthesize mapView = _mapView;
@synthesize locationManager = _locationManager;

- (id)init {
    self = [super init];
    if (nil != self) {
        _mapView = nil;
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    _locationManager = [[CLLocationManager alloc] init];
    // 位置情報サービスが利用できるかどうかをチェック
    if ([CLLocationManager locationServicesEnabled]) {
        _locationManager.delegate = self;
        // 測位開始
        [_locationManager startUpdatingLocation];
    } else {
        NSLog(@"Location services not available.");
    }
    
    CGRect main_ret = [[UIScreen mainScreen] bounds];
    
    float bar_height = main_ret.size.height / 12;
    
    CGRect ret = CGRectMake(0, bar_height, main_ret.size.width, main_ret.size.height - bar_height);
    
    self.view.frame = ret;
    
    _mapView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, main_ret.size.width, main_ret.size.height - bar_height)];
    
    [self.view addSubview:_mapView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
	_mapView = nil;
}

- (void)dealloc {
    [_locationManager release];
    [_mapView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self onResume];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self onPause];
}

- (void) onResume {
    if (nil == _locationManager && [CLLocationManager locationServicesEnabled])
        [_locationManager startUpdatingLocation]; //測位再開
}

- (void) onPause {
    if (nil == _locationManager && [CLLocationManager locationServicesEnabled])
        [_locationManager stopUpdatingLocation]; //測位停止
}

#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
// 位置情報更新時
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    _location = [locations lastObject];
    
//    NSString *googleSearch = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%@",[_location coordinate].latitude, [_location coordinate].longitude, luodan_address];
    
//    NSURL *url = [[NSURL alloc] initWithString:googleSearch];
//    [[UIApplication sharedApplication] openURL:url];
    //获取路线
//    NSMutableString *googleSearch = [NSMutableString stringWithFormat:@"http://ditu.google.cn/maps?f=d&source=s_d&saddr=%f,%f&daddr=%@&hl=zh&t=m&dirflg=h", [_location coordinate].latitude, [_location coordinate].longitude, luodan_address];
//    [_mapView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[googleSearch stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
}

// 測位失敗時や、5位置情報の利用をユーザーが「不許可」とした場合などに呼ばれる
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError");
}

@end
